package ai.people.netmon.framework.utils;

import ai.people.netmon.framework.exception.enums.ExceptionEnum;
import ai.people.netmon.framework.exception.type.BusinessException;
import ai.people.netmon.framework.model.response.Result;

import java.util.Objects;

/**
 * 断言
 *
 * @author yuanqinglong
 * @date 2022/2/17 15:29
 */
public class AssertUtils {


    /**
     * 结果为true
     *
     * @param compare       比较
     * @param exceptionEnum 异常枚举
     */
    public static void isTrue(boolean compare, ExceptionEnum exceptionEnum) {
        if (!compare) {
            throw new BusinessException(exceptionEnum);
        }
    }

    /**
     * 结果为true
     *
     * @param compare 比较
     * @param code    代码
     * @param message 消息
     */
    public static void isTrue(boolean compare, Integer code, String message) {
        if (!compare) {
            throw new BusinessException(message, code);
        }
    }


    /**
     * 结果为true
     *
     * @param result 结果
     */
    public static void isTrue(Result<?> result) {
        if (!result.getStatus()) {
            throw new BusinessException(result.getMsg(), result.getCode());
        }
    }


    /**
     * 结果为false
     *
     * @param compare       比较
     * @param exceptionEnum 异常枚举
     */
    public static void isFalse(boolean compare, ExceptionEnum exceptionEnum) {
        isTrue(!compare, exceptionEnum);
    }

    /**
     * 结果为true
     *
     * @param compare 比较
     * @param code    代码
     * @param message 消息
     */
    public static void isFalse(boolean compare, Integer code, String message) {
        if (!compare) {
            throw new BusinessException(message, code);
        }
    }

    /**
     * 结果不能为空
     *
     * @param compare       比较
     * @param exceptionEnum 异常枚举
     */
    public static void isNotNull(Object compare, ExceptionEnum exceptionEnum) {
        if (Objects.isNull(compare)) {
            throw new BusinessException(exceptionEnum);
        }
    }

    /**
     * 结果为true
     *
     * @param compare 比较
     * @param code    代码
     * @param message 消息
     */
    public static void isNotNull(Object compare, Integer code, String message) {
        if (Objects.isNull(compare)) {
            throw new BusinessException(message, code);
        }
    }

    /**
     * 结果为空
     *
     * @param compare       比较
     * @param exceptionEnum 异常枚举
     */
    public static void isNull(Object compare, ExceptionEnum exceptionEnum) {
        if (Objects.nonNull(compare)) {
            throw new BusinessException(exceptionEnum);
        }
    }

    /**
     * 结果为空
     *
     * @param compare 比较
     * @param code    代码
     * @param message 消息
     */
    public static void isNull(Object compare, Integer code, String message) {
        if (Objects.nonNull(compare)) {
            throw new BusinessException(message, code);
        }
    }
}
